package com.xiangxiangjd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiangxiangjd.bean.TrUser;
import com.xiangxiangjd.bean.Unit;
import com.xiangxiangjd.controller.helper.$;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.xiangxiangjd.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import com.xiangxiangjd.service.RoleService;
import com.xiangxiangjd.bean.Role;
import com.xiangxiangjd.controller.model.ApiResultModel;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2021-06-11
 */
@Controller
@RequestMapping("/api/role")
@Api(tags = "角色")
public class RoleController extends BaseController {


    /**
     * 分页查询Role
     *
     * @param page  页码
     * @param limit 查几条
     * @param role  前端传进的参数
     * @return 返回统一分页信息
     */
    @PostMapping(value = "admin/findRoleByPage")
    @ResponseBody
    @ApiOperation(value = "分页查询角色", notes = "分页查询角色", produces = "application/json")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ResultPage findRoleByPage(Integer page, Integer limit, Role role) {
        return roleService.findRoleByPage(page, limit, role);
    }

    /**
     * 添加Role
     *
     * @param role 前端传进的参数
     * @return 返回统一信息
     */
    @PostMapping(value = "admin/addRole")
    @ResponseBody
    @ApiOperation(value = "添加角色", notes = "添加角色", produces = "application/json")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ApiResultModel saveRole(Role role) {
        if (role.getFatherId().equals("0")) {
            List<Role> list = roleService.list(new LambdaQueryWrapper<Role>().eq(Role::getRoleName, role.getRoleName()));
            if (list.size() > 0) return $.error("该角色已存在");
        }
        return roleService.addRole(role);
    }

    /**
     * 编辑Role
     *
     * @param role 前端传进的参数
     * @return 返回统一信息
     */
    @PostMapping(value = "admin/editRole")
    @ResponseBody
    @ApiOperation(value = "编辑角色", notes = "编辑角色", produces = "application/json")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ApiResultModel editRole(Role role) {

        System.err.println(role.toString());

        if (role.getRoleName() == null) {
            return $.error("角色名称不可为空");
        }
        if (role.getFatherId() == null) {
            return $.error("父级id 不可为空");
        }

        if (role.getFatherId().equals("0")) {
            List<Role> list = roleService.list(new LambdaQueryWrapper<Role>().eq(Role::getRoleName, role.getRoleName()));
            if (list.size() > 0) return $.error("该大类已存在");

        }


        return roleService.editRole(role);
    }


    /**
     * 删除Role
     *
     * @param ids 前端传进的ID
     * @return 返回统一信息
     */
    @PostMapping(value = "admin/delRole")
    @ResponseBody
    @ApiOperation(value = "删除角色", notes = "删除角色", produces = "application/json")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ApiResultModel delRole(@RequestParam("datas") ArrayList ids) {


        for (int i = 0; i < ids.size(); i++) {
            List<TrUser> list = trUserService.list(new LambdaQueryWrapper<TrUser>().eq(TrUser::getRoleId, ids.get(i)));
            if (list.size() > 0) {
                return $.error("该角色已有用户 在使用不可删除");
            }
        }


        return roleService.delRoles(ids);
    }


    /**
     * 获取角色树
     *
     * @param
     * @return
     */
    @GetMapping(value = "/getTreeRole")
    @ResponseBody
    @ApiOperation(value = "获取角色树行结构", notes = "获取角色树行结构", produces = "application/json")
    @PreAuthorize("hasRole('ROLE_USER')")
    public ApiResultModel getTreeRole() {

        List<Role> data = roleService.getTreeRole();


        return $.success(data);
    }


}
